package top.ijiujiu.component;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;
import top.ijiujiu.util.ResultUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 处理注销成功
 *
 * @Author 神秘的黄瓜
 * @Version 1.0
 * @Date 2020-04-01 11:46
 */
@Component
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {

    private final static Logger LOGGER = LoggerFactory.getLogger(LogoutSuccessHandlerImpl.class);

    /**Json转化工具*/
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    @SuppressWarnings("deprecation")
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException{
        LOGGER.info("登出成功");
        ResultUtil<Boolean> result = new ResultUtil<>();
        result.success("登出成功", true);
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        response.getWriter().write(objectMapper.writeValueAsString(result));
    }
}
